﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class HangGiaCongDAO
    {
        private static QUANLYTIEMVANGEntities dbContext = new QUANLYTIEMVANGEntities();

        public static HANGGIACONG SelectHangGiaCongByMaHangGiaCong(HANGGIACONG hgc)
        {
            HANGGIACONG result = dbContext.HANGGIACONGs.Find(hgc.MaHangGiaCong);
            return result;
        }

        public static List<HANGGIACONG> GetAllHangGiaCong()
        {
            var query = from d in dbContext.HANGGIACONGs
                        select d;

            if (query.ToList().Count == 0)
                return null;

            return query.ToList();
        }

        public static void Insert(HANGGIACONG hgc)
        {
            dbContext.HANGGIACONGs.Add(hgc);
            dbContext.SaveChanges();
        }

        public static void Delete(HANGGIACONG hgc)
        {
            if (dbContext.HANGGIACONGs.Find(hgc.MaHangGiaCong) == null)
                return;

            dbContext.HANGGIACONGs.Remove(hgc);
            dbContext.SaveChanges();
        }

        public static void Update(HANGGIACONG hgc)
        {
            HANGGIACONG tmpHgc = dbContext.HANGGIACONGs.Find(hgc.MaHangGiaCong);
            if (tmpHgc != null)
            {
                tmpHgc.TenHangGiaCong = hgc.TenHangGiaCong;
                tmpHgc.DonGiaGiaCong = hgc.DonGiaGiaCong;
            }
        }

        public static int GetLastMaHangGiaCong()
        {
            var query = (from d in dbContext.HANGGIACONGs
                         select d.MaHangGiaCong).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
